<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>

    </style>
</head>

<body>

    <div id="box">


    </div>


    <script src="./js/react.js"></script>
    <script src="./js/react-dom.js"></script>
    <script src="./js/prop-types.js"></script>
    <script src="./js/babel.min.js"></script>

    <script type="text/babel">


        class Book extends React.Component {

            constructor() {
                super();
                this.state = {
                    name: "三国演义",
                    price: 51.2
                }
            }

            upPrice = () => {

                // 1、修改状态后，拿到新值
                // this.setState({
                //     price:this.state.price+1
                // },()=>{
                //     console.log("this.state.price",this.state.price); 
                // });

                // 2、修改状态前（修改状态的前一刻），拿到旧值                

                // this.setState(()=>{
                //     console.log("this.state.price",this.state.price); 
                //     return {
                //         price:this.state.price+1
                //     }
                // })


                //3、修改状态前（修改状态的前一刻），拿到旧值 ，修改状态后，拿到新值
                this.setState(() => {
                    console.log("旧的：this.state.price", this.state.price);
                    return {
                        price: this.state.price + 1
                    }
                }, () => {
                    console.log("新的：this.state.price",this.state.price); 
                });
            }

            render = () => {
                console.log("render");
                return (
                    <div>
                        <p>书名：{this.state.name}</p>
                        <p>价格：{this.state.price}</p>
                        <input type="button" value="涨价" onClick={this.upPrice} />
                    </div>
                )
            }
        }

        let price = 12.5;

        const root = (<div>
            <h1>组件：</h1>
            <Book author="李泽辉" price={price} />
        </div>
        );

        ReactDOM.render(root, document.getElementById("box"));


    </script>
</body>

</html>